﻿using System.IO;
using System.Text;

namespace Koala.Pro.Common
{
    public class LogTextHelper
    {
        private static string LogFolder;

        public static bool InfoLevel;

        public static bool DebugLevel;

        static LogTextHelper()
        {
            LogFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log");
            InfoLevel = true;
            DebugLevel = false;
            if (!Directory.Exists(LogFolder))
            {
                Directory.CreateDirectory(LogFolder);
            }
        }

        public static void WriteLine(string message)
        {
            string value = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]    ") + message + "\r\n\r\n";
            string path = DateTime.Now.ToString("yyyyMMdd") + ".log";
            try
            {
                if (InfoLevel)
                {
                    using FileStream stream = new FileStream(Path.Combine(LogFolder, path), FileMode.Append);
                    using StreamWriter streamWriter = new StreamWriter(stream, Encoding.Default);
                    streamWriter.Write(value);
                }
                if (DebugLevel)
                {
                    Console.WriteLine(value);
                }
            }
            catch
            {
            }
        }

        public static void WriteLine(string message, Exception ex)
        {
            string value = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]    ") + message + "\r\n" + ex.ToString() + "\r\n\r\n";
            string path = DateTime.Now.ToString("yyyyMMdd") + ".log";
            try
            {
                if (InfoLevel)
                {
                    new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
                    using FileStream stream = new FileStream(Path.Combine(LogFolder, path), FileMode.Append);
                    using StreamWriter streamWriter = new StreamWriter(stream, Encoding.Default);
                    streamWriter.Write(value);
                }
                if (DebugLevel)
                {
                    Console.WriteLine(value);
                }
            }
            catch
            {
            }
        }

        public static void WriteLine(string className, string funName, string message)
        {
            WriteLine($"{className}：{funName}\r\n{message}");
        }

        public static void Debug(object ex)
        {
            WriteLine(ex.ToString());
        }

        public static void Warn(object ex)
        {
            WriteLine(ex.ToString());
        }

        public static void Error(object ex)
        {
            WriteLine(ex.ToString());
        }

        public static void Info(object ex)
        {
            WriteLine(ex.ToString());
        }

        public static void Debug(object message, Exception ex)
        {
            WriteLine(message.ToString(), ex);
        }

        public static void Warn(object message, Exception ex)
        {
            WriteLine(message.ToString(), ex);
        }

        public static void Error(object message, Exception ex)
        {
            WriteLine(message.ToString(), ex);
        }

        public static void Info(object message, Exception ex)
        {
            WriteLine(message.ToString(), ex);
        }
    }
}
